﻿var myClick = false;
function actionActivity(item) { //红包活动
    $.ajax({
        url: "/app/Marketing/GetMarketingState",
        success: function (getState) {
            if (getState.b == 1) { //  返回 activity:活动数据 是第一次扫码
                $.ajax({
                    type: "get",
                    url: '/app/WebTemplate/GetAwards',
                    data: { address: address },
                    async: false,
                    success: function (res) {
                        var Denomination = res.Denomination;
                        if (res.Denomination == -1) { //没有中到奖品
                            $(item).children("img:nth-child(2)").attr("src", "/app/Content/Images/GoldEggs/hit.png").css("display", "block");
                            $("#small").css("animation-play-state", "paused");
                            $("span").css("animation-play-state", "paused");
                            setTimeout(function () {
                                $(item).children("img:nth-child(1)").attr("src", "/app/Content/Images/GoldEggs/egg2.png");

                                setTimeout(function () {
                                    GetError(TemplateID);
                                }, 1000)
                            }, 300);
                        }
                        else {  //中到奖品了
                            $('.bg-con').children('.small-bg').children('a.add').addClass('animated  zoomIn');
                            if (res.type == 3) { //流量
                                $(item).children("img:nth-child(2)").attr("src", "/app/Content/Images/GoldEggs/hit.png").css("display", "block");
                                $("#small").css("animation-play-state", "paused");
                                $("span").css("animation-play-state", "paused");
                                setTimeout(function () {
                                    $(item).children("img:nth-child(1)").attr("src", "/app/Content/Images/GoldEggs/egg2.png");

                                    setTimeout(function () {
                                        //res.Denomination = res.Name;
                                        $('#awardImg').attr("src", res.AwardsIosUrl)
                                        $(".bg-con .small-bg p:nth-child(2) span").text('价值' + res.Denomination + '元流量券').css({ 'width': 'auto', 'padding': '0 5px', 'color': '#E23524' });
                                        $(".bg-con .small-bg p:nth-child(3) span").text(res.Name);
                                        $('.bg-con .add').attr('href', 'Verification?awId=' + res.Id + '&Aid=' + res.ActivityId + '&Denomination=' + res.Denomination);
                                    }, 1000)
                                }, 300);
                                myClick = true; //一次抽奖完成后，设置click为true，可继续抽奖
                                actionActivity(aid, item);
                                return false;
                            }
                            else if (res.type == 2) {//优惠券
                            }
                            else { //红包
                                if (myClick) { //click控制一次抽奖过程中不能重复点击抽奖按钮，后面的点击不响应
                                    return false;
                                } else {
                                    $(item).children("img:nth-child(2)").attr("src", "/app/Content/Images/GoldEggs/hit.png").css("display", "block");
                                    $("#small").css("animation-play-state", "paused");
                                    $("span").css("animation-play-state", "paused");
                                    setTimeout(function () {
                                        $(item).children("img:nth-child(1)").attr("src", "/app/Content/Images/GoldEggs/egg2.png");

                                        setTimeout(function () {
                                            //$('#awardImg').attr("src", res.AwardsIosUrl)
                                            $(".bg-con .small-bg p:nth-child(2) span").text(res.Denomination + '元').css({ 'width': 'auto', 'padding': '0 10px', 'color': '#E23524' });
                                            $(".bg-con .small-bg p:nth-child(3) span").text(res.Name);
                                            $('.bg-con .add').attr('href', 'Verification?awId=' + res.Id + '&Aid=' + res.ActivityId + '&Denomination=' + res.Denomination);
                                            $('#bg-con>div:nth-child(2)').addClass("receive");
                                            $('#bg-con').css('display', 'flex');
                                        }, 1000)
                                    }, 300);
                                    myClick = true; //一次抽奖完成后，设置click为true，可继续抽奖
                                    actionActivity(aid, item);
                                    return false;
                                }
                            }
                        }
                    }
                });

            }
            else if (getState.b == 2) {//有活动 奖品已被领取
                var Nickname = decode(getState.record.Nickname);
                GetYet(TemplateID, Nickname);
            }
            else if (getState.b == 3) {//有活动 活动被扫码过  但用户没有领取 两次扫码已确定是同一个人  返回   activity:活动数据， Awards：中奖奖项 , record：中奖记录

                if (getState.record.Denomination == -1) { //没有中到奖品
                    GetError(TemplateID);
                }
                else {  //中到奖品了
                    $('.bg-con').children('.small-bg').children('a.add').addClass('animated  zoomIn');
                    if (getState.Awards.type == 3) { //流量
                        //getState.record.Denomination = getState.Name || '流量包';
                        $('#awardImg').attr("src", res.AwardsIosUrl)
                        $(".bg-con .small-bg p:nth-child(2) span").text('价值' + res.Denomination + '元流量券').css({ 'width': 'auto', 'padding': '0 5px', 'color': '#E23524' });
                        $(".bg-con .small-bg p:nth-child(3) span").text(res.Name);
                        $('.bg-con .add').attr('href', 'Verification?awId=' + res.Id + '&Aid=' + res.ActivityId + '&Denomination=' + res.Denomination);
                    }
                    else if (getState.Awards.type == 2) {//优惠券
                    }
                    else { //红包
                        if (myClick) { //click控制一次抽奖过程中不能重复点击抽奖按钮，后面的点击不响应
                            return false;
                        } else {
                            $(item).children("img:nth-child(2)").attr("src", "/app/Content/Images/GoldEggs/hit.png").css("display", "block");
                            $("#small").css("animation-play-state", "paused");
                            $("span").css("animation-play-state", "paused");
                            setTimeout(function () {
                                $(item).children("img:nth-child(1)").attr("src", "/app/Content/Images/GoldEggs/egg2.png");

                                setTimeout(function () {
                                    //$('#awardImg').attr("src", res.AwardsIosUrl)
                                    $(".bg-con .small-bg p:nth-child(2) span").text(res.Denomination + '元').css({ 'width': 'auto', 'padding': '0 10px', 'color': '#E23524' });
                                    $(".bg-con .small-bg p:nth-child(3) span").text(res.Name);
                                    $('.bg-con .add').attr('href', 'Verification?awId=' + res.Id + '&Aid=' + res.ActivityId + '&Denomination=' + res.Denomination);
                                    $('#bg-con>div:nth-child(2)').addClass("receive");
                                    $('#bg-con').css('display', 'flex');
                                }, 1000)
                            }, 300);


                            myClick = true; //一次抽奖完成后，设置click为true，可继续抽奖
                            actionActivity(aid, item);
                            return false;
                        }
                    }

                }
            }
        }
    });
}